package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.record.CountBussinessActivityDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.activity.CountBussinessActivity;
import com.syni.mdd.yhd.common.all.service.BmsBusinessService;
import com.syni.mdd.yhd.common.all.service.record.CountBussinessActivityService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.Optional;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class CountBussinessActivityServiceImpl extends BaseService<CountBussinessActivity, Integer> implements CountBussinessActivityService {

    @Autowired
    CountBussinessActivityDao countBussinessActivityDao;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Override
    public CountBussinessActivityDao getBaseDao() {
        return countBussinessActivityDao;
    }

    @Override
    public Map<String, Object> getScanCodeNum(Integer businessId) {
        Optional<BmsBusiness> bmsBusinessOptional = bmsBusinessService.getBaseDao().findById(businessId);
        if(!bmsBusinessOptional.isPresent()){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_NO_EXIST, ResultMsg.CODE_BUSINESS_NO_EXIST);
        }
        CountBussinessActivity countBussinessActivity = countBussinessActivityDao.findByBusinessId(businessId);
        countBussinessActivity = initCountBussinessActivity(countBussinessActivity,bmsBusinessOptional.get());
        countBussinessActivity.setScannerNum(countBussinessActivity.getScannerNum() + 1);
        countBussinessActivityDao.save(countBussinessActivity);
        return ResultMapHelper.success(null,"ok");
    }

    public CountBussinessActivity initCountBussinessActivity(CountBussinessActivity countBussinessActivity, BmsBusiness bmsBusiness){
        if(countBussinessActivity == null){
            countBussinessActivity = new CountBussinessActivity();
            countBussinessActivity.setScannerNum(Constants.COMMON_FLAG_FALSE_INT);
            countBussinessActivity.setRegisterNum(Constants.COMMON_FLAG_FALSE_INT);
            countBussinessActivity.setBusinessName(bmsBusiness.getVendorName());
            countBussinessActivity.setBusinessId(bmsBusiness.getId());
            countBussinessActivity.setNumber(Constants.COMMON_FLAG_FALSE_INT);
            countBussinessActivity.setUseNum(Constants.COMMON_FLAG_FALSE_INT);
            countBussinessActivity.setTotalMoney(0d);
            countBussinessActivity.setComMoney(0d);
            countBussinessActivity.setSubMoney(0d);
            countBussinessActivity.setNewTime(new Date());
            countBussinessActivityDao.save(countBussinessActivity);
        }
        return countBussinessActivity;
    }

    @Override
    @Async
    public synchronized void handleRecommendBusiness(Integer recommendBusinessId) {
        Optional<BmsBusiness> bmsBusinessOptional = bmsBusinessService.getBaseDao().findById(recommendBusinessId);
        if(bmsBusinessOptional.isPresent()){
            CountBussinessActivity countBussinessActivity = countBussinessActivityDao.findByBusinessId(recommendBusinessId);
            countBussinessActivity = initCountBussinessActivity(countBussinessActivity,bmsBusinessOptional.get());
            countBussinessActivity.setRegisterNum(countBussinessActivity.getRegisterNum() + 1);
            countBussinessActivityDao.save(countBussinessActivity);
        }
    }
}
